<template>
  <div class="approval">
    <el-card class="box-card">
      <div class="title">{{$t('achievements.title1')}}</div>
      <div class="menu-search">
        <div class="menu-title">
          <!-- 发送时间 -->
          <div class="searchItem searchItem-date">
            <span>{{$t('SMSRecord.sendTime')}}</span>
            <el-date-picker
              size="small"
              v-model="listQuery.crtTimeBegine"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('rule.beginDate')"
            ></el-date-picker>
            <b>{{$t('approval.go')}}</b>
            <el-date-picker
              size="small"
              v-model="listQuery.crtTimeEnd"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('rule.endDate')"
            ></el-date-picker>
          </div>
          <!-- 手机号 -->
          <div class="searchItem">
            <span>{{$t('approval.phoneNo')}}</span>
            <el-input
              v-model="listQuery.userPhone"
              :placeholder="$t('rule.phoneNo')"
              size="small"
            ></el-input>
          </div>
          <!-- 发送状态 -->
          <div class="searchItem">
            <span>{{$t('SMSRecord.sendStatus')}}</span>
            <el-select v-model="listQuery.sendCode" size="small">
              <el-option
                v-for="item in sendStatusArr"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </div>
        </div>
        <div class="menu-title-btn">
          <button type="button" @click="search">{{$t('btn.query')}}</button>
          <button
            type="button"
            @click="handleDownload"
            :disabled="expDis"
            v-if="smsSendGroup_btn_export"
          >{{$t('btn.export')}}</button>
        </div>
      </div>
    </el-card>
    <el-card class="box-card" style="margin-top: 10px;margin-bottom: 20px;">
      <div class="title">{{$t('DailyStatistics.ListDetails')}}</div>
      <el-table
        v-loading="listLoading"
        ref="multipleTable"
        :data="tableData"
        style="width: 100%"
        border
        size="mini"
      >
        <el-table-column :label="$t('user.num')" header-align="center" align="center" width="60">
          <template slot-scope="scope">
            <span>{{scope.row.index}}</span>
          </template>
        </el-table-column>
        <!-- 手机号 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="userPhone"
          width="140"
          :label="$t('approval.phoneNo')"
        ></el-table-column>
        <!-- 短信内容 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="smsContent"
          :label="$t('SMSRecord.smsCont')"
        ></el-table-column>
        <!-- 短信说明 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="smsXplain"
          width="180"
          :label="$t('SMSRecord.smsExplain')">
        </el-table-column>
        <!-- 发送时间 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="crtTime"
          width="180"
          :label="$t('SMSRecord.sendTime')"
        ></el-table-column>
        <!-- 发送状态 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="sendCode"
          width="180"
          :label="$t('SMSRecord.sendStatus')"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.sendCode === '0'">{{$t("SMSRecord.sendSus")}}</span>
            <span v-if="scope.row.sendCode !== '0'">{{$t('SMSRecord.sendFail')}}</span>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.page"
          :page-sizes="[5, 10, 20, 30]"
          :page-size="listQuery.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { dataMasking } from 'utils/index';
import {
  smsRecord,
  smsRecordExcel
} from "api/synManager/SMSRecord/index";
export default {
  data() {
    return {
      appChannel: [],
      smsSendGroup_btn_export: false,
      listLoading: false,
      total: null,
      expDis: true,
      listQuery: {
        limit: 5,
        page: 1,
        crtTimeBegine: "",
        crtTimeEnd: "",
        userPhone: "",
        sendCode: "",
        allSend: "1"
      },
      tableData: [],
      sendStatusArr: [
        {
          value: "",
          label: this.$t("rule.all")
        },
        {
          value: "0",
          label: this.$t("SMSRecord.sendSus")
        },
        {
          value: "1",
          label: this.$t("SMSRecord.sendFail")
        }
      ]
    };
  },
  computed: {
    ...mapGetters(["elements", "smsSendGroupPage"])
  },
  created() {
    if (this.smsSendGroupPage.has(this.$route.path)) {
      this.listQuery = this.smsSendGroupPage.get(this.$route.path);
    }
    this.getTime();
    this.getList();
    this.smsSendGroup_btn_export = this.elements["smsSendGroup:btn_export"];
  },
  methods: {
    getTime() {
      this.listQuery.crtTimeBegine = this.GetDateStr(0);
      this.listQuery.crtTimeEnd = this.GetDateStr(0);
    },
    GetDateStr(AddDayCount) {
      let dd = new Date();
      dd.setDate(dd.getDate() + AddDayCount);// 获取AddDayCount天后的日期
      let y = dd.getFullYear();
      let m = (dd.getMonth() + 1) < 10 ? '0' + (dd.getMonth() + 1) : (dd.getMonth() + 1); // 获取当前月份的日期，不足10补0
      let d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate(); // 获取当前几号，不足10补0
      return y + '-' + m + '-' + d;
    },
    // 点击导出
    handleDownload() {
      this.listLoading = true;
      smsRecordExcel(this.listQuery).then(res => {
        res.data.forEach(item => {
          // 手机号加掩码
          if (item.userPhone) {
            item.userPhone = dataMasking(item.userPhone)
          }
          // 发送状态
          if (item.sendCode === '0') {
            item.sendCode = this.$t("SMSRecord.sendSus")
          } else if (item.sendCode !== '0'){
            item.sendCode = this.$t("SMSRecord.sendFail")
          }
        })
        require.ensure([], () => {
          const { export_json_to_excel } = require("@/vendor/Export2Excel");
          const tHeader = [
            this.$t('approval.phoneNo'),
            this.$t('SMSRecord.smsCont'),
            this.$t('SMSRecord.smsExplain'),
            this.$t('SMSRecord.sendTime'),
            this.$t('SMSRecord.sendStatus')
          ];
          const filterVal = [
            "userPhone",
            "smsContent",
            "smsXplain",
            "crtTime",
            "sendCode"
          ];
          const list = res.data;
          const data = this.formatJson(filterVal, list);
          export_json_to_excel(tHeader, data, this.$t("SMSRecord.smsSendGroup"));
          this.listLoading = false;
        });
      });
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]));
    },
    getList() {
      if (this.listQuery.crtTimeBegine === '' || this.listQuery.crtTimeEnd === ''
      || this.listQuery.crtTimeBegine === null || this.listQuery.crtTimeEnd === null
      || this.listQuery.crtTimeBegine === undefined || this.listQuery.crtTimeEnd === undefined) {
        this.$message({
          showClose: true,
          type: "warning",
          message: this.$t('SMSRecord.choiceSendDate')
        });
      } else {
        this.listLoading = true;
        this.$store.dispatch("savesmsSendGroupPage", {
          path: this.$route.path,
          listQuery: this.listQuery
        });
        smsRecord(this.listQuery).then(res => {
          res.data.records.forEach((item, index) => {
            item.index =
              index + (this.listQuery.page - 1) * this.listQuery.limit + 1;
          });
          this.expDis = false;
          this.listLoading = false;
          this.tableData = res.data.records;
          this.total = Number(res.data.total);
        });
      }
    },
    //查询
    search() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    }
  }
};
</script>

<style scoped lang='scss'>
.title {
  line-height: 30px;
  font-size: 16px;
  display: flex;
  align-items: center;
  font-weight: bold;
}
.title:before {
  display: block;
  content: "";
  width: 3px;
  height: 16px;
  background-color: #309afe;
}
.title:before {
  margin-right: 8px;
}
::before {
  box-sizing: inherit;
  -webkit-tap-highlight-color: transparent;
}
.menu-title {
  width: 100%;
  box-sizing: border-box;
  background: #ffffff;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .searchItem {
    display: flex;
    align-items: center;
    padding: 10px 0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 150px;
    }
  }
  .searchItem-date {
    display: flex;
    align-items: center;
    padding: 10px 0;
    b {
      margin: 0 10px;
      font-weight: normal;
      font-size: 12px;
      color: #5f5f61;
    }
    div {
      width: 150px;
    }
  }
}
.menu-title-btn {
  padding: 10px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
    &:nth-child(2) {
      background: #ebf5ff;
      color: #20a0ff;
      border: 1px solid #abd5ff;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.menu-list {
  padding: 20px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
</style>